import 'dart:io';

import 'package:dmz/business/home/controller/home_main_controller.dart';
import 'package:dmz/business/home/controller/set_moto_controller.dart';
import 'package:dmz/routers/routers.dart';
import 'package:dmz/tools/payment/alipay_purchase_manger.dart';
import 'package:dmz/tools/payment/ios_purchase_manger.dart';
import 'package:dmz/utils/customize_loding.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/get_rx.dart';

import '../../../base/controller/base_controller.dart';
import '../../../request/apis.dart';
import '../../../request/request_client.dart';
import '../model/purchase_model.dart';

class SetPurchaseController extends BaseController {
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  @override
  void onInit() {
    getCommodityList();
    super.onInit();
  }

  List<CommodityModel> pageDatas = [];

  RxInt selecIndex = 0.obs;
  RxBool agreed = false.obs;

  getCommodityList() {
    RequestClient().get<List<CommodityModel>>(APIS.feeList,
        queryParameters: {}).then((value) {
      if (value != null) pageDatas = value;
      update(['purchase']);
      if (Platform.isIOS) {
        List<String> ids =
            pageDatas.map((element) => element.id ?? '').toList();
        EMIAppIosPurchaseManger().getProducts(Set.from(ids));
      }
    });
  }

  selectChange(int index) {
    selecIndex.value = index;
  }

  chnangeAgree(int type) {
    if (type == 0) {
      agreed.value = !agreed.value;
    } else {
      ///跳转支付协议
    }
  }

  ///开始支付
  didpurchase() {
    // if (Platform.isIOS) {
    //   EMIAppIosPurchaseManger().didcsbuy(pageDatas[selecIndex.value].id ?? '',
    //       (purchaseID) => purchaseVerification(purchaseID));
    // } else {
    //   Get.toNamed(RouteGet.payment);
    // }
    if (!agreed.value) return EMILoding.hitLoding('请阅读和同意支付协议！'.tr);
    Get.toNamed(RouteGet.payment);
  }

  ///ios 支付验证
  purchaseVerification(String purchaseID) {
    var homeController = Get.find<HomePageController>();
    var data = homeController.motoList[homeController.pageIndex];
    RequestClient().post<String>(APIS.applePayVerify, data: {
      'transaction_id': purchaseID,
      'ebike_id': data.id
    }).then((value) {
      EMILoding.hitLoding('续费成功'.tr, isScuess: true);
      Get.find<SetMotoController>().getServiceStatus();
      Get.back();
    });
  }

  ///安卓获取支付订单
  didPrepay() {
    var controller = Get.find<HomePageController>();
    RequestClient().post<Map>(APIS.prepay, data: {
      'channel': 'ali',
      'bike_id': controller.motoList[controller.pageIndex].id,
      'product_id': pageDatas[selecIndex.value].id
    }).then((value) {
      debugPrint('获取支付订单 prepay_id --- $value');
      if (value != null) {
        EMIAlipayPurchaseManger().didPay(value['prepay_id'], (scuess) {
          androidVerify(value['trade_no']);
        });
      }
    });
  }

  ///安卓支付验证
  androidVerify(String tradeNo) {
    RequestClient().post<Map>(APIS.androidPayVerify,
        data: {'trade_no': tradeNo}).then((value) {
      if (value != null) EMILoding.hitLoding('续费成功'.tr, isScuess: true);
      Get.find<SetMotoController>().getServiceStatus();
      Get.until((route) => route.settings.name == RouteGet.setMoto);
    });
  }
}
